package com.yanti.repository;

import com.yanti.domain.entity.mongodb.Conversation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.util.Calendar;
import java.util.Date;

public interface ConversationRepository extends MongoRepository<Conversation, String> {
    @Query("{ 'uid': ?0, 'assistantType': ?1, 'deleted': 0, 'lastUpdated': { $gt: ?2 } }")
    Page<Conversation> findRecentActiveConversations(
        String uid,
        String assistantType,
        Date cutoffDate,
        Pageable pageable
    );

    default Page<Conversation> findRecentActiveConversations(
        String uid, 
        String assistantType,
        Pageable pageable) {
        
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -2);
        return findRecentActiveConversations(
            uid,
            assistantType,
            calendar.getTime(),
            pageable
        );
    }
}